package com.bq.server.controller;

import cn.hutool.core.io.FileUtil;
import com.bq.server.base.Result;
import com.bq.server.core.model.CadreAppointInfo;
import com.bq.server.core.vo.request.AdjustDeptRequest;
import com.bq.server.core.vo.request.CadreAppointInfoRequest;
import com.bq.server.exception.BusinessException;
import com.bq.server.properties.MybatisPlusSecurityProperties;
import com.bq.server.service.CadreAppointInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.groups.Default;
import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
 * 干部任免审批
 *
 * @author chensh
 * @date 2021年10月14日 11:06 上午
 */
@Slf4j
@RestController
@RequestMapping("/admin/cadre")
@RequiredArgsConstructor
public class CadreController {


    private final MybatisPlusSecurityProperties securityProperties;

    public static String absolutePath = "";

    private final CadreAppointInfoService cadreAppointInfoService;

    /**
     * 添加
     *
     * @return 成功/失败
     */
    @PostMapping("/create")
    public Result<?> create(@RequestBody @Validated(CadreAppointInfoRequest.MvcGroup.class) CadreAppointInfoRequest request) {
        cadreAppointInfoService.create(request);
        return Result.success();
    }

    /**
     * 修改
     *
     * @return 成功/失败
     */
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Long id,
                            @RequestBody @Validated CadreAppointInfoRequest request) {
        cadreAppointInfoService.updateById(id, request);
        return Result.success();
    }

    /**
     * 删除
     *
     * @return 成功/失败
     */
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable Long id) {
        cadreAppointInfoService.remove(id);
        return Result.success();
    }

    /**
     * * 调整员工部门
     *
     * @param request 修改信息
     */
    @PostMapping("/adjust-dept")
    public Result<?> adjustDept(@RequestBody AdjustDeptRequest request) {
        cadreAppointInfoService.adjustDept(request);
        return Result.success();
    }

    /**
     * * 查看干部信息
     *
     * @param id 干部唯一ID
     */
    @GetMapping("/{id}")
    public Result<CadreAppointInfo> info(@PathVariable Long id) {
        CadreAppointInfo response = cadreAppointInfoService.info(id);
        return Result.success(response);
    }

    /**
     * 处理文件上传
     */
    @PostMapping(value = "/upload-img")
    public Result<String> uploadImg(@RequestParam("file") MultipartFile file) {
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        try {
            createDirIfNotExists();
            FileUtil.writeBytes(file.getBytes(), securityProperties.getFolder() + fileName);
        } catch (Exception e) {
            log.error("上传异常={}", e.getMessage(), e);
            throw BusinessException.of(1, "上传失败");
        }
        return Result.success("/img/" + fileName);
    }

    /**
     * 创建文件夹路径
     */
    private void createDirIfNotExists() {
        if (!absolutePath.isEmpty()) {
            return;
        }
        System.out.println(securityProperties.getFolder());
        File file = new File(securityProperties.getFolder());
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                absolutePath = securityProperties.getFolder();
            }
        }
    }

    /**
     * 删除图片
     *
     * @param filePath 图片地址
     */
    @DeleteMapping("/delete-img")
    public Result<?> deleteImages(String filePath) {
        File file = new File("/img/" + filePath);
        FileUtil.del(file);
        return Result.success();
    }

    @PostMapping("/{deptId}/import-word")
    public Result<?> importWord(@RequestParam("file") MultipartFile file,
                                @PathVariable("deptId") Long deptId) {
        cadreAppointInfoService.importWord(file, deptId);
        return Result.success();
    }

}